/*
 * MainWindow.cpp
 *
 *  Created on: 01.04.2009
 *      Author: Konan Andrej
 */

#include "MainWindow.h"

MainWindow::MainWindow()
{
	Print( "MainWindow constructor is executed" );

	createActions();
	createMenus();
	createToolBars();
	createStausBar();

        dbms = new DBMS();

        /*DataBaseInfo DBInfo;
        DBInfo.ColumnHeaders.push_back( tr("Article") );
	DBInfo.ColumnHeaders.push_back( tr("Description") );
	DBInfo.ColumnHeaders.push_back( tr("Number") );
	DBInfo.ColumnHeaders.push_back( tr("Format") );

	DBInfo.DBName = "Profile";
	DBInfo.ItemsNumber = 0;
	DBInfo.KeyField = 0;
	DBInfo.RecordsNumber = 4;
	DBInfo.VisibleNumber = 4;

	dbms->CreateDatabase( DBInfo );
        return;*/

	dbms->ConnectDatabase( "Profile" );
	dbms->SetCurrentDatabase( "Profile" );

	DBTable = new StockTableView( dbms->CurrentDatabase(), this );
	//Print("Error");
	this->setCentralWidget( DBTable );

        //adjustSize();
        //setGeometry( QRect(0,0,1000,1000) );

        Print("Creating DBDialog");
	DBDialog = new DataBaseWindow( dbms->CurrentDatabase(), this );
	Print( "MainWindow constructor is ended" );

}

void MainWindow::dial()
{
        dbms->ConnectDatabase( "Profile" );
        DBDialog->exec();
        //DBDialog->show();
        dbms->CurrentDatabase()->SaveDatabaseToFile( "Profile" );
        //We have changed pointer to Database
        //but before StockModel got it
        //dbms->DisconnectDatabse( "Profile" );
        //dbms->ConnectDatabase( "Profile" );
}

void MainWindow::createActions()
{
    editDatabaseAction = new QAction( tr("Edit database"), this );
    connect(editDatabaseAction, SIGNAL(triggered()), this, SLOT(dial()) );

    editEventsAction = new QAction( tr("Edit events"), this );

    newMaterialsAction = new QAction( tr("Add materials"), this );
    newMaterialsAction->setIcon( QIcon("images/icons/add.png") );
    newMaterialsAction->setStatusTip(tr("Add material"));

    newOrderAction = new QAction( tr("Add order"), this );
    newOrderAction->setIcon( QIcon("images/icons/del.png") );

    printAction = new QAction( tr("Print"), this );
    printAction->setIcon( QIcon("images/icons/print.png") );
    printAction->setShortcut( tr("Ctrl+P") );

    exitAction = new QAction( tr("Exit"), this );
    exitAction->setIcon( QIcon("images/icons/power.png") );
    //exitAction->setShortcut(tr("Alt+X"));
    connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
}

void MainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu( tr("File") );
    fileMenu->addAction( newMaterialsAction );
    fileMenu->addAction( newOrderAction );
    fileMenu->addSeparator();
    fileMenu->addAction( printAction );
    fileMenu->addSeparator();
    fileMenu->addAction( exitAction );

    databaseMenu = (menuBar())->addMenu(tr("Database"));
    databaseMenu->addAction( editDatabaseAction );
    databaseMenu->addAction( editEventsAction );
}

void MainWindow::createToolBars()
{
    fileToolBar = addToolBar(tr("File"));
    fileToolBar->addAction(newMaterialsAction);
    fileToolBar->addAction(newOrderAction);
    fileToolBar->addAction(printAction);
}

void MainWindow::createStausBar()
{
    statusBarLabel = new QLabel;
    statusBar()->addWidget( statusBarLabel );
}
